<template>
  <div>
    <el-upload
      class="avatar-uploader"
      action=""
      :class="{disabled:fileList.length > 0}"
      :file-list="fileList"
      list-type="picture-card"
      :on-success="handleAvatarSuccess"
      :before-upload="onBeforeUpload"
      :on-preview="handlePictureCardPreview"
      :on-remove="handleRemove"
      :on-progress="onProress"
      :on-change="onChange"
      :http-request="onUpload"
    >

      <i class="el-icon-plus avatar-uploader-icon"></i>
    </el-upload>
    <el-progress v-show="showPercent" :percentage="percent" style="width:198px"></el-progress>
    <el-dialog title="图片预览" :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>

    <!-- <div class="a disabled"><div class="b"></div></div>
    <div class="a"><div class="b"></div></div>
    <div class="disabled"><div class="b"></div></div> -->
  </div>
</template>

<script>
var COS = require('cos-js-sdk-v5')

// SECRETID 和 SECRETKEY请登录 https://console.cloud.tencent.com/cam/capi 进行查看和管理
var cos = new COS({
  SecretId: 'AKID97oFQKxIMN6YiaBVYJnzuZx4uTklW0C7',
  SecretKey: 'g5C2btue7IBn8BxSgdIgmb94j7D2ZqqY'
})
export default {
  name: 'UploadImage',
  components: {},
  props: {
  },
  data() {
    return {
      fileList: [{ url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100' }],
      dialogImageUrl: '',
      dialogVisible: false,
      showPercent: true,
      percent: 0
    }
  },
  created() {},
  mounted() {},
  methods: {
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    },
    handleRemove(file, fileList) {
      console.log(file, fileList)
      this.fileList = []
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url
      this.dialogVisible = true
    },
    onProress(event, file, fileList) {
      console.log(event, file, fileList)
    },
    onChange(file, fileList) {
      this.fileList.push({ url: file.url })
    },
    onBeforeUpload({ type, size }) {
      if (!['image/jpeg', 'image/gif', 'image/bmp', 'image/png'].includes(type)) {
        this.$message.error('文件类型不对')
      }
      if (size > 5 * 1024 * 1024) {
        this.$message.error('文件太大')
      }
    },
    onUpload({ file }) {
      this.showPercent = true
      cos.uploadFile({
        Bucket: 'renzizhongtai-1313030819', /* 填写自己的bucket，必须字段 */
        Region: 'ap-nanjing', /* 存储桶所在地域，必须字段 */
        Key: Date.now() + file.name, /* 存储在桶里的对象键（例如:1.jpg，a/b/test.txt，图片.jpg）支持中文，必须字段 */
        Body: file, // 上传文件对象
        SliceSize: 1024 * 1024 * 5, /* 触发分块上传的阈值，超过5MB使用分块上传，小于5MB使用简单上传。可自行设置，非必须 */
        onProgress: function(progressData) {
          console.log(JSON.stringify(progressData))
          this.percent = progressData.percent * 100
        }
      }, (err, data) => {
        this.showPercent = false
        if (err) {
          console.log('上传失败', err)
        } else {
          console.log('上传成功', data)
          this.fileList[0].url = 'http://' + data.Location
          console.log(this.fileList[0].url)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
//renzizhongtai-1313030819 ap-nanjing
//SecretId:AKID97oFQKxIMN6YiaBVYJnzuZx4uTklW0C7
//SecretKey:g5C2btue7IBn8BxSgdIgmb94j7D2ZqqY
//   .a .b{

//   }
// .a .disabled .b{

// }
// .a.disabled .b{

// }
// .disabled .b{

// }
::v-deep .avatar-uploader.disabled .el-upload--picture-card {
display: none;//隐藏元素
}
// ::v-deep样式穿透，不同的css语言用不同的样式穿透语法
// ::v-deep.avatar-uploader .el-upload {
//     border: 1px dashed #d9d9d9;
//     border-radius: 6px;
//     cursor: pointer;
//     position: relative;
//     overflow: hidden;
//   }
//   ::v-deep.avatar-uploader .el-upload:hover {
//     border-color: #409EFF;
//   }
//   ::v-deep.avatar-uploader-icon {
//     font-size: 28px;
//     color: #8c939d;
//     width: 178px;
//     height: 178px;
//     line-height: 178px;
//     text-align: center;
//   }
//   ::v-deep.avatar {
//     width: 178px;
//     height: 178px;
//     display: block;
//   }

</style>
